package com.wskeee.ec.managers
{
	import com.wskeee.ec.logger.Logger;
	import com.wskeee.ec.utils.ResourceLoader;
	
	import flash.display.Loader;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.ByteArray;

	public class ResourceManager
	{
		private var _data:Array = [];
		private var loader:ResourceLoader = new ResourceLoader();
		public function loadSkin(items:Array,complete:Function=null,progress:Function=null,ioError:Function=null):void
		{
			_data = items;
			var urls:Array = [];
			var item:Object;
			for(var i:int;i<items.length;i++)
			{
				item = items[i];
				item["domain"] = new ApplicationDomain(ApplicationDomain.currentDomain);
				urls.push({url:item["url"],domain:item["domain"]});
			}
			loader.appdomain = new ApplicationDomain();
			loader.loads(urls,complete,progress,ioError);
		}
		
		/**
		 * 追加加载任务 
		 * @param url		{url:xx,domain:ApplicationDomain}
		 * @param complete	Fun
		 * @param progress	Fun
		 * @param ioError	Fun
		 * 
		 */
		public function addLoad(name:String,url:String,complete:Function = null,progress:Function = null,ioError:Function = null):void
		{
			_data.push({name:name,url:url});
			loader.addLoad({url:url,domain:new ApplicationDomain(ApplicationDomain.currentDomain)},complete,progress,ioError);
		}
		
		/**
		 * 外部添加资源 
		 * @param name		资源名/路径，可通过查找资源
		 * @param content	资源本身
		 * @param domain	资源所处的域，防止资源冲突
		 * 
		 */
		public function addResource(content:Object,name:String="ecskin",domain:ApplicationDomain=null):void
		{
			if(!domain && content is Loader)
				domain = Loader(content).contentLoaderInfo.applicationDomain;
			var item:Object = getItemByName(name);
			if(!item){
				item = {name:name,url:name,domain:domain};
				_data.push(item);
			}
			loader.addResource(name,content);
		}
		
		/**
		 * 获取 skin 
		 * @param url
		 * @return 
		 * 
		 */		
		public function getSkin(url:String):Object
		{
			var skin:Object = loader.skin[url];
			return skin ? skin : url;
		}
		
		public function getSkinCopy(url:String):Object
		{
			var l:Loader = new Loader();
			var lc:LoaderContext = new LoaderContext(false,loader.appdomain);
			
			var d:ByteArray = loader.skindata[url];
			if(!d)return url;
			l.loadBytes(d,lc);
			
			return l;
		}
		
		/**
		 * 获取皮肤数据 
		 * @param url
		 * @return 
		 * 
		 */
		public function getSkinDataByName(name:String):Object
		{
			var item:Object = getItemByName(name);
			if(item)
			{
				var skin:ByteArray = loader.skindata[item["url"]];
				if(skin)return skin;
			}
			return null;
		}
		
		/**
		 * 通过 name 来获取url skin 
		 * @param name
		 * @return 
		 * 
		 */		
		public function getSkinByName(name:String,copy:Boolean = false):Object
		{
			var item:Object = getItemByName(name);
			if(item)
			{
				return copy ? getSkinCopy(item["url"]) : getSkin(item["url"]);
			}
			return null;
		}
		
		/**
		 * 获取元件的类 
		 * @param elemName
		 * @param name		资源名称
		 * @return 
		 * 
		 */
		public function getElementClass(elemName:String,name:String=""):Object
		{
			var item:Object = getItemByName(name);
			var domain:ApplicationDomain;
			if(item)
			{
				domain = item["domain"];
			}
			if(!domain)domain = loader.appdomain;
			var ec:Object = null;
			try
			{
				ec = domain.getDefinition(elemName)
			} 
			catch(error:Error) {}
			return ec;
		}
		
		/**
		 * 获取 对应item 
		 * @param name
		 * @return 
		 * 
		 */		
		private function getItemByName(name:String):Object
		{
			var item:Object;
			for(var i:int;i<data.length;i++)
			{
				item = data[i];
				if(item["name"] == name)return item;
			}
			return null;
		}
		
		private function get data():Array
		{
			return _data;
		}
	}
}